﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using System.IO;

namespace ScrumMonster
{
    public delegate void OnChange();

    public class Project
    {
        private string _name;
        private Guid _guid;
        private ScrumMonsterContext _context;
        private List<Sprint> _sprints = new List<Sprint>();
                
        public event OnChange OnProjectChange;

        public string Name
        {
            get
            {
                return _name;
            }
            
            internal set
            {
                _name = value;  
            }
        }

        public Guid GUID
        {
            get
            {
                return _guid;
            }

            internal set
            {
                _guid = value;
            }
        }

        public string XmlFileName
        {
            get
            {
                return string.Format("{0}{1}", this._guid.ToString(), ".xml");
            }
        }

        public ScrumMonsterContext Context
        {
            get
            {
                return _context;
            }

            internal set
            {
                _context = value;
            }
        }

        public XElement XElement
        {
            get
            {
                return new XElement("project",
                                      new XElement("name", this._name),
                                      new XElement("guid", this._guid),
                                      new XElement("sprints", from sprint in _sprints
                                                              select sprint.XElement
                                      ));
                
            }
        }

        /// <summary>
        /// Gets the sprints of this project
        /// </summary>
        public ReadOnlyCollection<Sprint> Sprints
        {
            get
            {
                return _sprints.AsReadOnly();
            }
        }

        internal Project(XElement element)
        {
            this.Name = element.Elements("name").First().Value;
            this.GUID = new Guid(element.Elements("guid").First().Value);
            this._sprints = new List<Sprint>( from sprint in element.Elements("sprints").Elements()
                                              select new Sprint(sprint)
                            );

        }

        internal Project(string name, Guid guid)
        {
            _name = name;
            _guid = guid;
        }

        public static Project CreateNewProject(string name)
        {
            throw new NotImplementedException();
        }

        public Sprint AddSprint(TimeSpan estimatedTime)
        {
            Sprint sprint = new Sprint(estimatedTime);

            this._sprints.Add(sprint);

            this.Context.WriteToFile(this);
            
            OnProjectChange();

            return sprint;
        }


    }
}
